<?php
/**
 * Created by .
 * User: luogan
 * Date: 2022/1/14
 */

namespace app\model;

use hg\apidoc\annotation\Field;
use hg\apidoc\annotation\AddField;
class MenuModel extends BaseModel
{
    protected $table = 'api_menu';
    protected $pk = 'id';
    protected $autoWriteTimestamp=true;
    protected $hidden=['delete_time'];

    /**
     * 获取菜单详情
     * @Field("id,title,icon,type,parent_id,component,path,action,module,controller,method,sort,target,status,create_time,update_time")
     */
    public function getMenu(){}

    /**
     * 获取菜单列表（最多为三级）
     * @Field("id,title,icon,type,parent_id,component,path,action,sort,status")
     * @AddField("deep",type="int",desc="分类深度",)
     */
    public function getMenuTreeList($data,$showDeep = 5,$field ='*')
    {
        //获取所有的菜单
        $classList = $this->field('id,title,icon,type,parent_id,component,path,action,module,sort,status')
            ->order('parent_id','asc')
            ->order('sort','desc')
            ->order('id','asc')
            ->select()
            ->toArray();
        $goodsClass = [];//分类数组
        if (is_array($classList) && !empty($classList)) {
            $showDeep = intval($showDeep);
            if ($showDeep == 1) {//只显示第一级时用循环给分类加上深度deep号码
                foreach ($classList as $val) {
                    if ($val['parent_id'] == 0) {
                        $val['deep'] = 1;
                        $goodsClass[] = $val;
                    } else {
                        break;//父类编号不为0时退出循环
                    }
                }
            } else {//显示第二和三级时用递归
                $goodsClass = $this->getTreeClassList($showDeep, $classList);
            }
        }
        return $goodsClass;
    }


}
